package com.sukaiyi.giteeideaplugin.mainwindow;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.components.JBLabel;
import com.sukaiyi.giteeideaplugin.utils.HumanizedDateUtils;
import icons.PluginIcons;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Optional;

public class DeleteRepoConfirmWindow extends DialogWrapper {
    private JPanel content;
    private JBLabel iconLabel;
    private JBLabel starIconLabel;
    private JBLabel watchIconLabel;
    private JBLabel forkIconLabel;
    private JBLabel descriptionLabel;
    private JBLabel nameLabel;
    private JBLabel languageLabel;
    private JBLabel pushedAtLabel;

    public DeleteRepoConfirmWindow(JSONObject repo) {
        super(true);
        setTitle("是否确认删除此仓库?");
        init();
        nameLabel.setText(repo.getStr("human_name"));
        boolean isPrivate = repo.getBool("private", false);
        boolean isPublic = repo.getBool("public", false);
        boolean isInternal = repo.getBool("internal", false);
        boolean isFork = repo.getBool("fork", false);
        boolean stared = repo.getBool("stared", false);
        boolean watched = repo.getBool("watched", false);
        int stargazersCount = repo.getInt("stargazers_count", 0);
        int watchersCount = repo.getInt("watchers_count", 0);
        int forksCount = repo.getInt("forks_count", 0);
        String pushed_at = repo.getStr("pushed_at", "2020-06-22T11:47:59+08:00");

        iconLabel.setIcon(isPrivate ? PluginIcons.LOCK_DARK : PluginIcons.REPO_DARK);
        starIconLabel.setText(String.valueOf(stargazersCount));
        starIconLabel.setIcon(stared ? PluginIcons.STAR : IconLoader.getDisabledIcon(PluginIcons.STAR));
        watchIconLabel.setText(String.valueOf(watchersCount));
        watchIconLabel.setIcon(watched ? PluginIcons.WATCH : IconLoader.getDisabledIcon(PluginIcons.WATCH));
        forkIconLabel.setText(String.valueOf(forksCount));
        forkIconLabel.setIcon(PluginIcons.FORK);
        descriptionLabel.setText(StrUtil.maxLength(repo.getStr("description"), 30));
        languageLabel.setText(repo.getStr("language"));
        pushedAtLabel.setText(
                Optional.of(pushed_at)
                        .map(at -> DateUtil.parse(at, "yyyy-MM-dd'T'HH:mm:ss'+08:00'"))
                        .map(HumanizedDateUtils::humanize)
                        .orElse("")
        );
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        return content;
    }
}
